/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import java.awt.GridLayout;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;
import thecliffproblem.Action;
import thecliffproblem.Board;
import thecliffproblem.Coordinate;

/**
 *
 * @author Jaime
 */
public class BoardPanel extends JPanel{
    
    private Board board;
    private SquarePanel[][] panels;
    private Lock lock;
    private final ApplicationPanel app;
    
    public BoardPanel(Board board, ApplicationPanel app)
    {
        lock =  new ReentrantLock();
        this.board = board;
        board.registerObserver(new BoardPanelObserver());
        this.setLayout(new GridLayout(board.getNumberOfLines(), board.getNumberOfColumns()));
        panels =  new SquarePanel[board.getNumberOfLines()][board.getNumberOfColumns()];
        
        createPanel();
        this.app = app;
        
        setBorder(new TitledBorder("Environment"));
        
    }

    private void createPanel() {
        for(int i = 0; i < board.getNumberOfLines(); i++)
        {
            for(int j = 0; j < board.getNumberOfColumns(); j++)
            {
                SquarePanel sp = new SquarePanel(board.getSquareType(new Coordinate(i, j)));
                add(sp);
                panels[i][j] = sp;
            }
        }
    }
   
    private void setType(Coordinate square, int type)
    {
        lock.lock();
        try {
            panels[square.getLine()][square.getColumn()].setType(type);
            panels[square.getLine()][square.getColumn()].repaint();
            
            
        } finally {
            lock.unlock();
        }
    }
    
    
    private void resetPanel()
    {
        lock.lock();
        try {
            int i = 0;
            int j = 0;
            for (SquarePanel[] sqs : panels) {
                j = 0;
                for (SquarePanel sq : sqs) {
                    sq.setType(board.getSquareType(new Coordinate(i, j)));
                    j++;
                }
                i++;
            }
        } finally {
            lock.unlock();
        }
    }

    void repaintAll() {
        
        for(int i = 0; i < board.getNumberOfLines(); i++)
        {
            for(int j = 0; j < board.getNumberOfColumns(); j++)
            {
                panels[i][j].revalidate();
                panels[i][j].repaint();

            }
        }
        
        repaint();
        revalidate();
    }
    
    private class BoardPanelObserver implements ActionObserver
    {

        @Override
        public void step(Coordinate origin, Coordinate destination) {
            
            setType(origin, board.getSquareType(origin) == 1 ? 1 : 5);
            setType(destination, board.getSquareType(destination));
            //app.repaintAll();
            
        }

        @Override
        public void episodeEnd() {
            resetPanel();
            //app.repaintAll();
        }

        @Override
        public void newEvaluation(Action action, Coordinate Square, double value) {
            
        }
        
    }
    
   
    
}
